package com.example.web;

import com.example.pojo.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/user")
@Slf4j
public class UserInfoController {
    @GetMapping("/")
    public String list(Model model){
        log.info("进入UserInfoController的list方法..........");
        //TODO 1.调用service查询数据
        UserInfo u1 = new UserInfo();
        u1.setId(1);
        u1.setAddress("五一新干线");
        u1.setUsername("旺财");
        u1.setBirthday(new Date(2018-1900,7-1,12));
        u1.setSex("女");

        UserInfo u2 = new UserInfo();
        u2.setId(2);
        u2.setAddress("泊富企业广场");
        u2.setUsername("来福");
        u2.setBirthday(new Date());

        u1.setSex("男");

        List<UserInfo> userInfoList = Arrays.asList(u1, u2);

        //TODO 2.返回model和view
        model.addAttribute("userInfoList", userInfoList);

        return "user/list";//   /templates/user/list.html

    }
//    @RequestMapping("/toAdd")
//    public String userAdd(){
//        return "user/userAdd";
//    }

    @RequestMapping("/add")
    public String add(UserInfo userInfo){
        log.info("进入UserInfoController的add方法，参数为：{}",userInfo);
        return "redirect:/user/";
    }

    @RequestMapping("/info")
    public String info(Integer uid,Model model){
        log.info("进入UserInfoController的info方法，参数为：{}",uid);
        //1.校验参数
        //2.调用service
        //3.返回数据和视图
        UserInfo u1 = new UserInfo();
        u1.setId(1);
        u1.setAddress("五一新干线");
        u1.setUsername("旺财");
        u1.setBirthday(new Date(2018-1900,7-1,12)); //模拟从数据库查询了id为1的用户信息
        u1.setSex("女");
        model.addAttribute("userInfo",u1);

        return "/user/userEdit";
    }

    @RequestMapping("/update")
    public String update(UserInfo userInfo){
        log.info("进入UserInfoController的update方法，参数为：{}",userInfo);
        //1.校验
        //2.调用service
        //3.返回数据和视图
        return "redirect:/user/";
    }
}
